package org.csu.mypetstore.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.LatestProduct;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.persistence.CategoryMapper;
import org.csu.mypetstore.persistence.ItemMapper;
import org.csu.mypetstore.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CategortService {
    @Autowired
    private CategoryMapper categoryDAO;

    @Autowired
    private ProductMapper productDAO;

    @Autowired
    private ItemMapper itemDAO;

    public int getQuanatity(String itemId){
        return itemDAO.getInventoryQuantity(itemId);
    }

    public List<Category> getCategoryList() {
        return categoryDAO.getCategoryList();
    }

    public Category getCategory(String categoryId) {
        return categoryDAO.getCategory(categoryId);
    }

    public Product getProduct(String itemId) {
        return productDAO.getProduct(productDAO.getProductId(itemId));
    }
    public Product getProductByname(String name){
        return productDAO.getProductByname(name);
    }
    public Product getProductByProductID(String productId){return productDAO.getProduct(productId);}

    public List<Product> getProductListByCategory(String categoryId) {
        return productDAO.getProductListByCategory(categoryId);
    }

    // TODO enable using more than one keyword
    public List<Product> searchProductList(String keyword) {
        return productDAO.searchProductList("%" + keyword.toLowerCase() + "%");
    }

    public List<Item> getItemListByProduct(String productId) {
        return itemDAO.getItemListByProduct(productId);
    }

    public Item getItem(String itemId) {
        return itemDAO.getItem(itemId);
    }

    public boolean isItemInStock(String itemId) {
        if(itemDAO.getInventoryQuantity(itemId)>0)
            return true;
        else
            return false;
    }

    public List<Product> getallproduct(){
        return productDAO.getallproduct();
    }
    public List<LatestProduct> getlatestproduct(){
        return itemDAO.selcetlatestproduct();
    }
    public List<Product> searchProduct(String keyword){
        return productDAO.searchProduct(keyword);
    }
    public void deleteCastegory(String itemId){
        categoryDAO.deleteCastegory(itemId);
    }
    public void insertCategory(Category category){
        categoryDAO.insertCategory(category);
    }
    public List<Category> searchCategory(String keyword){
        return categoryDAO.searchCategory(keyword);
    }
    public void deleteProduct(String productid){
        productDAO.deleteProduct(productid);
    }
    public void insertProduct(Product product){
        productDAO.insertProduct(product);
    }
    public List<Item> getallItem(){
        return itemDAO.getallItem();
    }
    public void deleteItem(String ItemId){
        itemDAO.deleteItem(ItemId);
        itemDAO.deletequantity(ItemId);
        itemDAO.deleteinstock(ItemId);
    }
    public void inserItem(Item item){
        itemDAO.inserItem(item);
        itemDAO.insertQuantity(item);
        item.setInstock("true");
        itemDAO.insertinstock(item);
    }
    public List<Item> searchItem(String keyword){
        return itemDAO.searchItem(keyword);
    }
    public void updateCategory(Category category){
        categoryDAO.updateCategory(category);
    }
    public void updateProdcut(Product product){
        productDAO.updateProduct(product);
    }
    public void updateItem(Item item){
        itemDAO.updateItem(item);
        itemDAO.updateQuantity(item);
        if(itemDAO.isExistitem(item.getItemId())!=null){
            itemDAO.updateItem(item);
        }
    }
    public void insertLatestProduct(LatestProduct latestProduct){
        itemDAO.insertLatestProduct(latestProduct);
    }
    public void deletelatestproduct(String itemid){
        itemDAO.deletelatestproduct(itemid);
    }
    public void updateinstock(String itemId,String instock){
        itemDAO.updateinstock(itemId,instock);
    }
    public void insertinstock(Item item){
        item.setInstock("true");
        itemDAO.insertinstock(item);
    }
    public String selectinstock(String itemId){
        return itemDAO.selectinstock(itemId);
    }

}